﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using ScormModel;
using ScormModel.View;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using ScormModel.HtmlBuilder;
namespace ScormModel.Question
{
    public class ChoiceQuestionData
    {
        string _questionContent;
        string _questionHint;
        string _type = "single";
        string _score;
        string _identifier;
        string currentDirectory;
        List<QuestionAnswer> _listPossibleAnswer;
        List<QuestionAnswer> _listFact;
        public string QuestionContent
        {
            get { return this._questionContent; }
            set { this._questionContent = value; }
        }
        public string Identifier
        {
            get { return this._identifier; }
            set { this._identifier = value; }
        }
        public string Type
        {
            get { return this._type; }
            set { this._type = value; }
        }
        public string Score
        {
            get { return this._score; }
            set { this._score = value; }
        }
        public string QuestionHint
        {
            get { return this._questionHint; }
            set { this._questionHint = value; }
        }
        public List<QuestionAnswer> ListPossibleAnswer
        {
            get { return this._listPossibleAnswer; }
            set { this._listPossibleAnswer = value; }
        }
        public List<QuestionAnswer> ListFact
        {
            get { return this._listFact; }
            set { this._listFact = value; }
        }
        public string CurrentDirectory
        {
            get { return this.currentDirectory; }
            set { this.currentDirectory = value; }
        }
        public HtmlDocument CreateHtmlNode()
        {
            ScormModel.HtmlBuilder.HtmlElement inputForm = new ScormModel.HtmlBuilder.HtmlElement("form");
            inputForm.Nodes.Clear();
            this.WriteQuestionHtmlCode(inputForm);
            if (_listFact != null && _listFact.Count > 0){
                this.WriteQuestionFact(inputForm);
            }
            this.WritePossibleAnswerHtmlCode(inputForm, _type);
            this.WriteSubmitButtonHtmlCode(inputForm);
            this.WriteDescriptionHtmlCode(inputForm);
            this.WriteJavaScriptHtmlCode(inputForm);
            StreamReader sr = new StreamReader(Directory.GetCurrentDirectory() + "\\QuestionTemplates\\" + "QuestionTemplatePage.htm", System.Text.UnicodeEncoding.UTF8);

            string readHtmlCode = sr.ReadToEnd();
            sr.Close();
            ScormModel.HtmlBuilder.HtmlDocument mDocument = ScormModel.HtmlBuilder.HtmlDocument.Create(readHtmlCode, true);//sr.ReadToEnd(),true);

            ScormModel.HtmlBuilder.HtmlElement htmlElement = (ScormModel.HtmlBuilder.HtmlElement)mDocument.Nodes["HTML"];
            ScormModel.HtmlBuilder.HtmlElement questionElement = (ScormModel.HtmlBuilder.HtmlElement)htmlElement.Nodes["BODY"];
            questionElement.Nodes.Add(inputForm);
            return mDocument;
        }

        public void createQuestion(ScormModel.HtmlBuilder.HtmlElement inputForm,int index)
        {
            this.writeQuestion(inputForm, index);
            if (_listFact != null && _listFact.Count > 0)
            {
                this.WriteQuestionFact(inputForm);
            }
            this.WritePossibleAnswerHtmlCode(inputForm, _type);
           // this.WriteSubmitButtonHtmlCode(inputForm);
            this.WriteDescriptionHtmlCode(inputForm);
            //this.WriteJavaScriptHtmlCode(inputForm);
        }
        private void writeQuestion(ScormModel.HtmlBuilder.HtmlElement inputForm,int index)
        {
            this._identifier = this._identifier + "_" + index;
            HtmlAttribute nameAttribute = new HtmlAttribute("name", this._identifier);
            inputForm.Attributes.Add(nameAttribute);
            HtmlText indexText = new HtmlText(this._questionContent);
            inputForm.Nodes.Add((ScormModel.HtmlBuilder.HtmlNode)indexText);
            ScormModel.HtmlBuilder.HtmlElement brtext = new ScormModel.HtmlBuilder.HtmlElement("br");
            inputForm.Nodes.Add((ScormModel.HtmlBuilder.HtmlNode)brtext);
        }
        private void WriteQuestionHtmlCode(ScormModel.HtmlBuilder.HtmlElement inputForm)
        {
            try
            {
                
                //write question html code
                HtmlAttribute nameAttribute = new HtmlAttribute("name", this._identifier);
                inputForm.Attributes.Add(nameAttribute);
                StreamReader sr = new StreamReader(Directory.GetCurrentDirectory() + "\\QuestionTemplates\\" + "QuestionTemplatePage.htm", System.Text.UnicodeEncoding.UTF8);

                string readHtmlCode = sr.ReadToEnd();
                sr.Close();
                ScormModel.HtmlBuilder.HtmlDocument mDocument = ScormModel.HtmlBuilder.HtmlDocument.Create(readHtmlCode, true);//sr.ReadToEnd(),true);

                ScormModel.HtmlBuilder.HtmlElement htmlElement = (ScormModel.HtmlBuilder.HtmlElement)mDocument.Nodes["HTML"];
                ScormModel.HtmlBuilder.HtmlElement questionElement = (ScormModel.HtmlBuilder.HtmlElement)htmlElement.Nodes["BODY"];
                questionElement.Name = "p";
                questionElement.Nodes.Add((HtmlNode)new HtmlText(this._questionContent));
                inputForm.Nodes.Add((ScormModel.HtmlBuilder.HtmlNode)questionElement);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        private void WriteQuestionFact(ScormModel.HtmlBuilder.HtmlElement inputForm)
        {
            for (int i = 0; i < this.ListFact.Count; i++)
            {
                HtmlText indexText = new HtmlText(((int)(i + 1)).ToString() + ". ");
                HtmlText possibleAnswerText = new HtmlText(((QuestionAnswer)this.ListFact[i]).Answer);
                ScormModel.HtmlBuilder.HtmlElement brElement = new ScormModel.HtmlBuilder.HtmlElement("br");
                inputForm.Nodes.Add((ScormModel.HtmlBuilder.HtmlNode)indexText);
                inputForm.Nodes.Add((ScormModel.HtmlBuilder.HtmlNode)possibleAnswerText);
                inputForm.Nodes.Add((ScormModel.HtmlBuilder.HtmlNode)brElement);
            }
            HtmlText text = new HtmlText("Choose the correct answer");
            ScormModel.HtmlBuilder.HtmlElement brtext = new ScormModel.HtmlBuilder.HtmlElement("br");
            inputForm.Nodes.Add((ScormModel.HtmlBuilder.HtmlNode)text);
            inputForm.Nodes.Add((ScormModel.HtmlBuilder.HtmlNode)brtext);
        }
        private void WritePossibleAnswerHtmlCode(ScormModel.HtmlBuilder.HtmlElement inputForm, string inputType)
        {
            switch (inputType)
            {
                case "simple":
                    #region simple
                    for (int i = 0; i < this._listPossibleAnswer.Count; i++)
                    {
                        ScormModel.HtmlBuilder.HtmlElement inputElement = new ScormModel.HtmlBuilder.HtmlElement("input");
                        HtmlAttribute typeAttribute = new HtmlAttribute("type", "Radio");
                        HtmlAttribute nameAttribute = new HtmlAttribute("name", "simpleChoice");
                        HtmlAttribute valueAttribute = new HtmlAttribute("value", ((int)(i + 1)).ToString());
                        inputElement.Attributes.Add(typeAttribute);
                        inputElement.Attributes.Add(nameAttribute);
                        inputElement.Attributes.Add(valueAttribute);
                        HtmlText possibleAnswerText = new HtmlText(((QuestionAnswer)this._listPossibleAnswer[i]).Answer);
                        ScormModel.HtmlBuilder.HtmlElement brElement = new ScormModel.HtmlBuilder.HtmlElement("br");
                        HtmlText indexText = new HtmlText(((int)(i + 1)).ToString() + ". ");
                        inputForm.Nodes.Add((ScormModel.HtmlBuilder.HtmlNode)indexText);
                        inputForm.Nodes.Add((ScormModel.HtmlBuilder.HtmlNode)inputElement);
                        inputForm.Nodes.Add((ScormModel.HtmlBuilder.HtmlNode)possibleAnswerText);
                        inputForm.Nodes.Add((ScormModel.HtmlBuilder.HtmlNode)brElement);
                    }
                    break;
                    #endregion
                case "true_false":
                    #region true_false
                    // True Radio Button
                    ScormModel.HtmlBuilder.HtmlElement inputElement1 = new ScormModel.HtmlBuilder.HtmlElement("input");
                    ScormModel.HtmlBuilder.HtmlAttribute typeAttribute1 = new ScormModel.HtmlBuilder.HtmlAttribute("type", "Radio");
                    HtmlAttribute nameAttribute1 = new HtmlAttribute("name", "true_false");
                    inputElement1.Attributes.Add(typeAttribute1);
                    inputElement1.Attributes.Add(nameAttribute1);
                    HtmlText possibleAnswerText1 = new HtmlText("True");
                    ScormModel.HtmlBuilder.HtmlElement brElement1 = new ScormModel.HtmlBuilder.HtmlElement("br");
                    HtmlText indexText1 = new HtmlText("1. ");
                    inputForm.Nodes.Add((ScormModel.HtmlBuilder.HtmlNode)indexText1);
                    inputForm.Nodes.Add((ScormModel.HtmlBuilder.HtmlNode)inputElement1);
                    inputForm.Nodes.Add((ScormModel.HtmlBuilder.HtmlNode)possibleAnswerText1);
                    inputForm.Nodes.Add((ScormModel.HtmlBuilder.HtmlNode)brElement1);
                    //False Radio Button
                    ScormModel.HtmlBuilder.HtmlElement inputElement2 = new ScormModel.HtmlBuilder.HtmlElement("input");
                    HtmlAttribute typeAttribute2 = new HtmlAttribute("type", "Radio");
                    HtmlAttribute nameAttribute2 = new HtmlAttribute("name", "true_false");
                    inputElement2.Attributes.Add(typeAttribute2);
                    inputElement2.Attributes.Add(nameAttribute2);
                    HtmlText possibleAnswerText2 = new HtmlText("False");
                    ScormModel.HtmlBuilder.HtmlElement brElement2 = new ScormModel.HtmlBuilder.HtmlElement("br");
                    HtmlText indexText2 = new HtmlText("2. ");
                    inputForm.Nodes.Add((HtmlNode)indexText2);
                    inputForm.Nodes.Add((HtmlNode)inputElement2);
                    inputForm.Nodes.Add((HtmlNode)possibleAnswerText2);
                    inputForm.Nodes.Add((HtmlNode)brElement2);
                    break;
                    #endregion
                case "multiple":
                    #region multiple
                    for (int i = 0; i < this._listPossibleAnswer.Count; i++)
                    {
                        ScormModel.HtmlBuilder.HtmlElement inputElement = new ScormModel.HtmlBuilder.HtmlElement("input");
                        HtmlAttribute typeAttribute = new HtmlAttribute("type", "checkbox");
                        HtmlAttribute nameAttribute = new HtmlAttribute("name", "mutipleChoice" + (i + 1).ToString());
                        inputElement.Attributes.Add(typeAttribute);
                        inputElement.Attributes.Add(nameAttribute);
                        HtmlText possibleAnswerText = new HtmlText(((QuestionAnswer)this._listPossibleAnswer[i]).Answer);
                        ScormModel.HtmlBuilder.HtmlElement brElement = new ScormModel.HtmlBuilder.HtmlElement("br");
                        HtmlText indexText = new HtmlText(((int)(i + 1)).ToString() + ". ");
                        inputForm.Nodes.Add((HtmlNode)indexText);
                        inputForm.Nodes.Add((HtmlNode)inputElement);
                        inputForm.Nodes.Add((HtmlNode)possibleAnswerText);
                        inputForm.Nodes.Add((HtmlNode)brElement);
                    }
                    break;
                    #endregion
                case "order":
                    break;
            }
        }

        private void WriteSubmitButtonHtmlCode(ScormModel.HtmlBuilder.HtmlElement inputForm)
        {
            //write submit button html code
            int temp = inputForm.Nodes.Add((HtmlNode)new ScormModel.HtmlBuilder.HtmlElement("p"));
            ScormModel.HtmlBuilder.HtmlElement inputElement = new ScormModel.HtmlBuilder.HtmlElement("input");
            HtmlAttribute typeAttribute = new HtmlAttribute("type", "button");
            HtmlAttribute nameAttribute = new HtmlAttribute("name", "submitButton");
            HtmlAttribute valueAttribute = new HtmlAttribute("value", " Submit ");
            HtmlAttribute onClickAttribute = new HtmlAttribute("onClick", "calcScore()");
            inputElement.Attributes.Add(typeAttribute);
            inputElement.Attributes.Add(nameAttribute);
            inputElement.Attributes.Add(valueAttribute);
            inputElement.Attributes.Add(onClickAttribute);
            ((ScormModel.HtmlBuilder.HtmlElement)inputForm.Nodes[temp]).Nodes.Add((HtmlNode)inputElement);
        }

        private void WriteDescriptionHtmlCode(ScormModel.HtmlBuilder.HtmlElement inputForm)
        {
            //write description html code
            ScormModel.HtmlBuilder.HtmlElement descriptionElement = new ScormModel.HtmlBuilder.HtmlElement("p");
            ScormModel.HtmlBuilder.HtmlElement italicElement1 = new ScormModel.HtmlBuilder.HtmlElement("i");
            italicElement1.Nodes.Add((HtmlNode)new HtmlText("Hint:"));
            ScormModel.HtmlBuilder.HtmlElement italicElement2 = new ScormModel.HtmlBuilder.HtmlElement("i");
            italicElement2.Nodes.Add((HtmlNode)new HtmlText(this._questionHint));
            descriptionElement.Nodes.Add((HtmlNode)italicElement1);
            descriptionElement.Nodes.Add((HtmlNode)new ScormModel.HtmlBuilder.HtmlElement("br"));
            descriptionElement.Nodes.Add((HtmlNode)italicElement2);
            inputForm.Nodes.Add((HtmlNode)descriptionElement);
        }
        private void WriteJavaScriptHtmlCode(ScormModel.HtmlBuilder.HtmlElement inputForm)
        {
            //write javascript html code
            ScormModel.HtmlBuilder.HtmlElement scriptElement = new ScormModel.HtmlBuilder.HtmlElement("script");
            HtmlAttribute languageAttribute = new HtmlAttribute("language", "javascript");
            HtmlText javaScriptCode = new HtmlText(this.CreateJavaScriptCode(inputForm));
            scriptElement.Attributes.Add(languageAttribute);
            scriptElement.Nodes.Add((HtmlNode)javaScriptCode);
            inputForm.Nodes.Add((HtmlNode)scriptElement);
        }
        private string CreateJavaScriptCode(ScormModel.HtmlBuilder.HtmlElement inputForm)
        {
            string Res = "";
            Res += "\nvar exitPageStatus;\nfunction loadPage()\n{\ndoInitialize();\nexitPageStatus = false;\n}\n";
            Res += "function unloadPage()\n{\nif(exitPageStatus != true)\n{\ncalcScore();\n}\n}\n";

            switch (this._type)
            {
                case "multiple":
                    Res += "function calcScore()\n{\n" + this._identifier + ".submitButton.disabled=true;\n" + "var result=true;\n";
                    for (int i = 0; i < this._listPossibleAnswer.Count; i++)
                    {
                        Res += "if(" + this._identifier + ".mutipleChoice" + ((int)(i + 1)).ToString() + ".checked!=";
                        if (((QuestionAnswer)(this._listPossibleAnswer[i])).Correct == true)
                        {
                            Res += "true)\n";
                        }
                        else
                        {
                            Res += "false)\n";
                        }
                        Res += "{\nresult=false;\n}\n";
                    }
                    Res += "if(result==true)\n{\ndoSetValue(\"cmi.success_status\",\"passed\");\ndoSetValue(\"cmi.score.scaled\",1);\ndoSetValue(\"cmi.objectives.0.success_status\",\"passed\");\n}\nelse\n{\ndoSetValue(\"cmi.success_status\",\"failed\");\ndoSetValue(\"cmi.score.scaled\",0);\ndoSetValue(\"cmi.objectives.0.success_status\",\"failed\");\n}\n";
                    Res += "exitPageStatus=true;\ndoTerminate()\n}\n";
                    break;
                case "simple":
                    Res += "function calcScore()\n{\n" + this._identifier + ".submitButton.disabled=true;\n";
                    Res += "if(" + this._identifier + ".simpleChoice[";
                    for (int i = 0; i < _listPossibleAnswer.Count; i++)
                    {
                        if (((QuestionAnswer)(_listPossibleAnswer[i])).Correct == true)
                        {
                            Res += i.ToString() + "].checked==true)\n";
                        }
                    }
                    Res += "{\ndoSetValue(\"cmi.success_status\",\"passed\");\ndoSetValue(\"cmi.score.scaled\",1.0);\ndoSetValue(\"cmi.objectives.0.success_status\",\"passed\");\n}\nelse\n{\ndoSetValue(\"cmi.success_status\",\"failed\");\ndoSetValue(\"cmi.score.scaled\",0);\ndoSetValue(\"cmi.objectives.0.success_status\",\"failed\");\n}\n";									
                    Res += "exitPageStatus=true;\ndoTerminate()\n}\n";
                    break;
                case "true_false":
                    Res += "function calcScore()\n{\n" + this._identifier + ".submitButton.disabled=true;\n";
                    Res += "if(" + this._identifier + ".true_false[";
                     for (int i = 0; i < _listPossibleAnswer.Count; i++)
                    {
                        if (((QuestionAnswer)(_listPossibleAnswer[i])).Correct == true)
                        {
                            Res += i.ToString() + "])\n";
                        }
                    }
                    Res += "].checked==true)\n";

                    Res += "{\ndoSetValue(\"cmi.success_status\",\"passed\");\ndoSetValue(\"cmi.score.scaled\",1);\ndoSetValue(\"cmi.objectives.0.success_status\",\"passed\");\n}\nelse\n{\ndoSetValue(\"cmi.success_status\",\"failed\");\ndoSetValue(\"cmi.score.scaled\",0);\ndoSetValue(\"cmi.objectives.0.success_status\",\"failed\");\n}\n";
                    Res += "exitPageStatus=true;\ndoTerminate()\n}\n";
                    break;
                case "order":
                      Res += "function calcScore()\n{\n" + this._identifier + ".submitButton.disabled=true;\n";
                    Res += "if(" + this._identifier + ".simpleChoice[";
                    for (int i = 0; i < _listPossibleAnswer.Count; i++)
                    {
                        if (((QuestionAnswer)(_listPossibleAnswer[i])).Correct == true)
                        {
                            Res += i.ToString() + "].checked==true)\n";
                        }
                    }
                    Res += "{\ndoSetValue(\"cmi.success_status\",\"passed\");\ndoSetValue(\"cmi.score.scaled\",1.0);\ndoSetValue(\"cmi.objectives.0.success_status\",\"passed\");\n}\nelse\n{\ndoSetValue(\"cmi.success_status\",\"failed\");\ndoSetValue(\"cmi.score.scaled\",0);\ndoSetValue(\"cmi.objectives.0.success_status\",\"failed\");\n}\n";									
                    Res += "exitPageStatus=true;\ndoTerminate()\n}\n";
                    break;
            }
            return Res;
        }
        
        public string CreateJavaScriptForTest()
        {
            string Res = "";
            //string tempScore = ((float)(this.score / 10)).ToString();
            int score = 1;
            switch (this._type)
            {
                case "multiple":
                    Res += "result=true;\n";
                    Res += "totalQuestion +=" + score.ToString() + " ;\n";
                    for (int i = 0; i < _listPossibleAnswer.Count; i++)
                    {
                        Res += "if(" + this._identifier + ".mutipleChoice" + ((int)(i + 1)).ToString() + ".checked!=";
                        if (((QuestionAnswer)(this._listPossibleAnswer[i])).Correct == true)
                        {
                            Res += "true)\n";
                        }
                        else
                        {
                            Res += "false)\n";
                        }
                        Res += "{\nresult=false;\n}\n";
                    }
                    Res += "if(result==true)\n{\ntotalScore+=" + score.ToString() + ";\n}\n";
                    break;
                case "simple":
                    Res += "totalQuestion += " + score.ToString() + ";\n";
                    Res += "if((" + this._identifier + ".simpleChoice)[";
                    for (int i = 0; i < _listPossibleAnswer.Count; i++)
                    {
                        if (((QuestionAnswer)(_listPossibleAnswer[i])).Correct == true)
                        {
                            Res += i.ToString() + "].checked==true)\n";
                        }
                    }
                    Res += "{\ntotalScore+=" + score.ToString() + ";\n}\n";
                    break;
                case "true_false":
                    Res += "totalQuestion += " + score.ToString() + ";\n";
                    Res += "if(" + this._identifier + ".true_false[";
                     for (int i = 0; i < _listPossibleAnswer.Count; i++)
                    {
                        if (((QuestionAnswer)(_listPossibleAnswer[i])).Correct == true)
                        {
                            Res += i.ToString() + "])\n";
                        }
                    }
                    Res += "].checked==true)\n";
                    Res += "{\ntotalScore+=" + score.ToString() + ";}\n";
                    break;
                case "order":
                    Res += "totalQuestion += " + score.ToString() + ";\n";
                    Res += "if((" + this._identifier + ".simpleChoice)[";
                    for (int i = 0; i < _listPossibleAnswer.Count; i++)
                    {
                        if (((QuestionAnswer)(_listPossibleAnswer[i])).Correct == true)
                        {
                            Res += i.ToString() + "].checked==true)\n";
                        }
                    }
                    Res += "{\ntotalScore+=" + score.ToString() + ";\n}\n";
                    break;
            }
            return Res;
        }
   
    }
}
